// pages/tieContent/tieContent.js
wx.cloud.init();
const db = wx.cloud.database();
var util = require('../../util/util');
var app = getApp()
Page({
  data: {
    addtext:'',
    inputShowed:false,
    sonOrfather:false,
    fatherName:'',
    sortway: ["时间","热度"],
    colorIndex: 1,
    content_datas:{},
    sortfather:[],
    goodson:[],
    thisson:'',
    rewrite:'',
    userInfo:'',
    change:false,
    changef:false,
    changes:false,
    temp:[],
// 
    content: "",
    appraisetemp:{
        appraiseuser:'',
        isOpen: false,
        change: false,
        response: 0,
        agreelist:[],
        hot:0,
        appraise_content: "",
        time: "",
        reply_list: [
        ],
    },
    appraisetempsons:{
      appraiseuser:'',
      change: false,
      hot:0,
      appraise_content: "",
      time: ""
  },

  },
  agree:function(e){
    app.globalData.change = true;
    var k = this.data.content_datas.hot+1
    this.setData({
      'content_datas.change':true,
      'content1_datas.change':true,
      'content_datas.hot':k,
      'content1_datas.hot':k,
      'change':true
    })
    let that = this
    db.collection('forum').where({
      _id:e.currentTarget.id
      }).update({
      data:{
        hot:db.command.inc(1),
        agree:db.command.push(that.data.userInfo.nickName),
        change:true
      },
    })
  },
  agree_concel:function(e){
    app.globalData.change = false;
    var k = this.data.content_datas.hot-1
    this.setData({
      'content_datas.change':false,
      'content1_datas.change':false,
      'content_datas.hot':k,
      'content1_datas.hot':k,
      'change':false
    })  
    let that = this
    db.collection('forum').where({
      _id:e.currentTarget.id
      }).update({
      data:{
        agree:db.command.pull(that.data.userInfo.nickName),
        hot:db.command.inc(-1),
        change:false
      },
    })
  },

  agreef:function(e){
    
   var k = this.data.content_datas.response_user[e.currentTarget.dataset.index].hot+1
    this.setData({
      ['content_datas.response_user['+ e.currentTarget.dataset.index +'].change']:true,
      ['content1_datas.response_user['+ e.currentTarget.dataset.index +'].change']:true,
      ['content_datas.response_user['+ e.currentTarget.dataset.index +'].hot']:k,
      ['content1_datas.response_user['+ e.currentTarget.dataset.index +'].hot']:k,
      'fatherName':e.currentTarget.id
    })
    
    this.data.sortfather.sort(this.compare1("ms_time"))
    for(var i = 0;i<this.data.sortfather.length;i++)
    {
      
      if(this.data.fatherName == this.data.sortfather[i].ms_time)
      {
        break;
      }
    }

    let that = this
    db.collection('forum').where({
      _id:app.globalData.index
      }).update({
      data:{
        ['response_user.'+ i +'.hot']:db.command.inc(1),
        ['response_user.'+ i +'.agreelist']:db.command.push(that.data.userInfo.nickName),
      },
    })
  },
  agreef_concel:function(e){
    var k = this.data.content_datas.response_user[e.currentTarget.dataset.index].hot-1
    this.setData({
      ['content_datas.response_user['+ e.currentTarget.dataset.index +'].change']:false,
      ['content1_datas.response_user['+ e.currentTarget.dataset.index +'].change']:false,
      ['content_datas.response_user['+ e.currentTarget.dataset.index +'].hot']:k,
      ['content1_datas.response_user['+ e.currentTarget.dataset.index +'].hot']:k,
      'fatherName':e.currentTarget.id
    })
    this.data.sortfather.sort(this.compare1("ms_time"))
    for(var i = 0;i<this.data.sortfather.length;i++)
    {
      if(this.data.fatherName == this.data.sortfather[i].ms_time)
      {
        break;
      }
    }
    let that = this
    db.collection('forum').where({
      _id:app.globalData.index
      }).update({
      data:{
        ['response_user.'+ i +'.hot']:db.command.inc(-1),
        ['response_user.'+ i +'.agreelist']:db.command.pull(that.data.userInfo.nickName),
      },
    })
  },

  agrees:function(e){
    
    for(var i =0;i<this.data.temp.length;i++)
    {
      if(e.currentTarget.id == this.data.temp[i].ms_time)
      {

        this.data.thisson =  this.data.temp[i].reply_list[e.currentTarget.dataset.index].ms_time

        var k = this.data.content_datas.response_user[i].reply_list[e.currentTarget.dataset.index].hot + 1
       
        this.setData({
          ['content_datas.response_user['+ i +'].reply_list['+e.currentTarget.dataset.index+'].hot']: k,
          ['content_datas.response_user['+ i +'].reply_list['+e.currentTarget.dataset.index+'].change']: true,
        })
        
        var a = this.data.temp[i].reply_list
        this.data.goodson = a.sort(this.compare("ms_time"))
       
        break;
      }

    }
    this.data.sortfather.sort(this.compare1("ms_time"))
    var i =0
    for(;i<this.data.sortfather.length;i++)
    {
      
      if(e.currentTarget.id  == this.data.sortfather[i].ms_time)
      {
        break;
      }
    }
  var j = 0
    for(;j<this.data.temp[i].reply_list.length;j++)
    {
      
      if(this.data.thisson == this.data.temp[i].reply_list[j].ms_time)
      {
        
        break;
      }
    }

    let that = this
    db.collection('forum').where({
      _id:app.globalData.index
      }).update({
      data:{
        ['response_user.'+ i +'.reply_list.'+j+'.hot']: db.command.inc(1),
        ['response_user.'+ i +'.reply_list.'+j+'.change']: true,
        ['response_user.'+ i +'.reply_list.'+j+'.agreelist']:db.command.push(that.data.userInfo.nickName)
      },
    })
    
  },
  agrees_concel:function(e){
    
    for(var i =0;i<this.data.temp.length;i++)
    {
      if(e.currentTarget.id == this.data.temp[i].ms_time)
      {
        
        this.data.thisson =  this.data.temp[i].reply_list[e.currentTarget.dataset.index].ms_time

        var k = this.data.temp[i].reply_list[e.currentTarget.dataset.index].hot - 1
        
        this.setData({
          ['content_datas.response_user['+ i +'].reply_list['+e.currentTarget.dataset.index+'].hot']: k,
          ['content_datas.response_user['+ i +'].reply_list['+e.currentTarget.dataset.index+'].change']: false,
        })
        
        var a = this.data.temp[i].reply_list
        this.data.goodson = a.sort(this.compare("ms_time"))
        
        break;
      }

    }
    this.data.sortfather.sort(this.compare1("ms_time"))
    var i = 0
    for(;i<this.data.sortfather.length;i++)
    {
      
      if(e.currentTarget.id  == this.data.sortfather[i].ms_time)
      {
        break;
      }
    }
    var j = 0
    for(;j<this.data.temp[i].reply_list.length;j++)
    {
      
      if(this.data.thisson == this.data.temp[i].reply_list[j].ms_time)
      {
        
        break;
      }
    }
    
    let that = this
    db.collection('forum').where({
      _id:app.globalData.index
      }).update({
      data:{
        ['response_user.'+ i +'.reply_list.'+j+'.hot']: db.command.inc(-1),
        ['response_user.'+ i +'.reply_list.'+j+'.change']: false,
        ['response_user.'+ i +'.reply_list.'+j+'.agreelist']:db.command.pull(that.data.userInfo.nickName)
      },
    })
  
  },

  deletef:function(e){
    let that = this
    wx.showModal({
      title: '提示',
      content: '您确定要删除吗',
      success (res) {

        if (res.confirm) {
          var i =0
          that.setData({
      'fatherName':e.currentTarget.id
    })
    
    for(;i<that.data.content_datas.response_user.length;i++)
    {
      if(that.data.fatherName == that.data.content_datas.response_user[i].ms_time)
      {
        var k = that.data.content_datas.response -1
        that.data.sortfather = that.data.content_datas.response_user
        that.data.content_datas.response_user.splice(i, 1)
        that.setData({
          'content_datas.response': k,
          'content_datas.response_user':that.data.content_datas.response_user
        })
        break;
      }
    }
    that.data.sortfather = that.data.content_datas.response_user
    that.data.sortfather.sort(that.compare1("ms_time"))
    
    db.collection('forum').doc(app.globalData.index)
      .update({
      data:{
        'response': db.command.inc(-1),
        'response_user':that.data.sortfather
      },
    })
    wx.showToast({
      icon:'success',
      title: '删除成功！'
    })
        
        } 
        else if (res.cancel) {
          wx.showToast({
            icon:'success',
            title: '小心手滑！'
          })
        }
      }
    })

    
  },

  deletes:function(e){
    let that = this;
    wx.showModal({
      title: '提示',
      content: '您确定要删除吗',
      success (res) {
      
        if (res.confirm) {
         
          for(var i =0;i<that.data.temp.length;i++)
          {
            if(e.currentTarget.id == that.data.temp[i].ms_time)
            {
             
              that.data.thisson =  that.data.temp[i].reply_list[e.currentTarget.dataset.index].ms_time
      
              var k = that.data.content_datas.response_user[i].response - 1
              that.data.content_datas.response_user[i].reply_list.splice(e.currentTarget.dataset.index, 1)
              that.setData({
                ['content_datas.response_user['+ i +'].response']: k,
                ['content_datas.response_user['+ i +'].reply_list']:that.data.content_datas.response_user[i].reply_list,
                'content_datas.response_user':that.data.content_datas.response_user
              })
              var a = that.data.temp[i].reply_list
              that.data.goodson = a.sort(that.compare("ms_time"))
             
              break;
            }
      
          }
          that.data.sortfather.sort(that.compare1("ms_time"))
          var i =0
          for(;i<that.data.sortfather.length;i++)
          {
            
            if(e.currentTarget.id  == that.data.sortfather[i].ms_time)
            {
              break;
            }
          }
        var j = 0
          for(;j<that.data.temp[i].reply_list.length;j++)
          {
            
            if(that.data.thisson == that.data.temp[i].reply_list[j].ms_time)
            {
              
              break;
            }
          }
          that.data.sortfather = that.data.content_datas.response_user
          that.data.sortfather.sort(that.compare1("ms_time"))

          db.collection('forum').where({
            _id:app.globalData.index
            }).update({
            data:{
              'response_user':that.data.sortfather
            },
          })

          wx.showToast({
          icon:'success',
          title: '删除成功！'
          })
        } 
        else if (res.cancel) {
          wx.showToast({
            icon: 'none',
            title: '没事别瞎点！'
          })
        }
      }
    })


    
   
  },

  compare: function (property) {
    return function (a, b) {
      var value1 = a[property]
      var value2 = b[property]
      return value2 - value1
    }
  },
  compare1: function (property) {
    return function (a, b) {
      var value1 = a[property]
      var value2 = b[property]
      return value1 - value2
    }
  },

  sort_:function(e){
    this.setData({
      tag: this.data.sortway[e.currentTarget.dataset.index],
      colorIndex: e.currentTarget.dataset.index,
    })
    if(e.currentTarget.dataset.index == 0)
    {
     
      this.data.sortfather.sort(this.compare("ms_time"))
      this.setData({
      'content_datas.response_user':(this.data.sortfather),
    })
    }
    else{
      this.data.sortfather.sort(this.compare("hot"))
      this.setData({
      'content_datas.response_user.':(this.data.sortfather),
    })
    }
  },


  Input_Title:function (e) {
    this.setData({
      rewrite: e.detail.value
    })
  },


  getFocus:function(e){
  
   this.setData({
     inputShowed:true,
     'fatherName':e.currentTarget.id,
     sonOrfather:true
   })
  },
  chooseUnfold: function(e) {
    var key = e.currentTarget.dataset.key;
    var val = e.currentTarget.dataset.value;
    key = key + '.isOpen';
    this.setData({
      [key]: !val
    })
  },

  emit:function(e){
    if(this.data.rewrite){
    let that = this
    let time = util.formatTime(new Date())
    let ms_time = new Date().getTime()
    if(!this.data.sonOrfather){
      this.setData({
        'appraisetemp.appraiseuser':this.data.userInfo,
        'appraisetemp.isOpen': false,
        'appraisetemp.change': false,
        'appraisetemp.response': 0,
        'appraisetemp.agreelist': [],
        'appraisetemp.hot': 0,
        'appraisetemp.appraise_content': this.data.rewrite,
        'appraisetemp.time': time,
        'appraisetemp.ms_time':ms_time,
        'appraisetemp.reply_list': [],
      })
      var temp = this.data.appraisetemp
     
      db.collection('forum').where({
        _id:app.globalData.index
        }).update({
        data:{
          response:db.command.inc(1),
          response_user:db.command.push(temp),
            // push({each:[temp],
            //   sort:{
            //     hot:-1,
            //     ms_time:-1
            //   }}),
        },
      })
     
      let a = []
    
      a.push(temp)
     
      a = a.concat(that.data.content_datas.response_user),
     
      this.setData({
        'inputShowed':false,
        'content_datas.response_user':a,
        'sortfather':this.data.content_datas.response_user,
        'content_datas.response':this.data.content_datas.response+1
      })
      wx.showToast({
        icon:'success',
        title: '回复成功了耶！'
      })
    }
  else{
  
    this.setData({
      'appraisetempsons.appraiseuser':this.data.userInfo,
      'appraisetempsons.change': false,
      'appraisetempsons.hot': 0,
      'appraisetempsons.delete':false,
      'appraisetempsons.agreelist':[],
      'appraisetempsons.appraise_content': this.data.rewrite,
      'appraisetempsons.time': time,
      'appraisetempsons.ms_time':ms_time
    })
    if(this.data.colorIndex == 1) 
    this.data.content_datas.response_user.sort(this.compare("hot"))
    var i =0
    for(;i<this.data.content_datas.response_user.length;i++)
    {
      if(this.data.fatherName == this.data.content_datas.response_user[i].ms_time)
      {
        
        var k = this.data.content_datas.response_user[i].response + 1
        var t = []
        t.push(this.data.appraisetempsons)
        // this.data.content_datas.response_user[i].reply_list[t] = this.data.appraisetempsons
       
        this.setData({
          ['content_datas.response_user['+ i +'].response']: k,
          ['content_datas.response_user['+ i +'].reply_list']:t.concat(this.data.content_datas.response_user[i].reply_list),
          'sortfather':this.data.content_datas.response_user
        })
        break;
      }
    }
   
    this.data.sortfather.sort(this.compare1("ms_time"))
    for(i = 0;i<this.data.sortfather.length;i++)
    {
     
      if(this.data.fatherName == this.data.sortfather[i].ms_time)
      {
        break;
      }
    }
   
    let temp = this.data.appraisetempsons
    db.collection('forum').where({
      _id:app.globalData.index,
      })
      .update({
      data:{
        ['response_user.'+i+'.response']:db.command.inc(1),
        ['response_user.'+i+'.reply_list']:db.command.push({each:[temp],position:0,
          sort:{
          hot:-1
        }})
      },
    })

    this.setData({
      sonOrfather:false
    })
    this.setData({
      rewrite:''
    })
    wx.showToast({
      icon:'success',
      title: '回复成功了耶！'
    })
    } 
    this.setData({
      addtext:''
    })
    
  }//endif
},
  /**
   * 生命周期函数--监听页面加载
   */
   
  onLoad(options) {
    let user=wx.getStorageSync('user')
   
    this.setData({
      userInfo:user
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
    let that = this
    db.collection('forum').where({
      _id:app.globalData.index
    })
    .get({
      success: function(res) {
        that.setData({
          content_datas:res.data[0],
          sortfather:res.data[0].response_user
       })
       that.data.sortfather.sort(that.compare("hot"))
       that.setData({
       'content_datas.response_user.':(that.data.sortfather),
       'change':app.globalData.change
       })
       for(var i=0;i<that.data.content_datas.response_user.length;i++)
       {
          that.setData({
          ['content_datas.response_user['+ i +'].change']:false,
          ['content_datas.response_user['+ i +'].delete']:false,
          })
          if(that.data.userInfo.nickName == that.data.content_datas.response_user[i].appraiseuser.nickName)
          {
            that.setData({
              ['content_datas.response_user['+ i +'].delete']:true,
              })
          }
          for(var j=0;j<that.data.content_datas.response_user[i].agreelist.length;j++)
          { 
            if(that.data.userInfo.nickName == that.data.content_datas.response_user[i].agreelist[j])
            {
              that.setData({
                 ['content_datas.response_user['+ i +'].change']:true,
            })
              break;
            }  
          }  
          
          for(var j=0;j<that.data.content_datas.response_user[i].reply_list.length;j++)
          {
            that.setData({
              ['content_datas.response_user['+ i +'].reply_list['+j+'].change']:false,
              ['content_datas.response_user['+ i +'].reply_list['+j+'].delete']:false,
              })
              if(that.data.userInfo.nickName == that.data.content_datas.response_user[i].reply_list[j].appraiseuser.nickName)
              {
                that.setData({
                  ['content_datas.response_user['+ i +'].reply_list['+j+'].delete']:true,
              })
              }
              
              for(var k=0;k<that.data.content_datas.response_user[i].reply_list[j].agreelist.length;k++)
              {
                if(that.data.userInfo.nickName == that.data.content_datas.response_user[i].reply_list[j].agreelist[k])
                {
                  that.setData({
                    ['content_datas.response_user['+ i +'].reply_list['+j+'].change']:true,
                })
                  break;
                }  
              }
          } 
          
          var a = that.data.content_datas.response_user[i].reply_list
          a.sort(that.compare("hot"))
         
          that.setData({
            ['content_datas.response_user['+ i +'].reply_list']:a
          })
       
       }   
      
       that.setData({
        'sortfather':(that.data.content_datas.response_user),
        'temp':(that.data.content_datas.response_user),
        'content_datas.response_user':that.data.content_datas.response_user
       })
      
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let that = this
    db.collection('forum').where({
      _id:app.globalData.index
    })
    .get({
      success: function(res) {
        that.setData({
          content_datas:res.data[0],
          // 'content_datas.time' : res.data[0].time.slice(0,15)
          sortfather:res.data[0].response_user
       })
       that.data.sortfather.sort(that.compare("hot"))
       that.setData({
       'content_datas.response_user.':(that.data.sortfather),
       'change':app.globalData.change
       })
       for(var i=0;i<that.data.content_datas.response_user.length;i++)
       {
          that.setData({
          ['content_datas.response_user['+ i +'].change']:false,
          })
          for(var j=0;j<that.data.content_datas.response_user[i].agreelist.length;j++)
          {
            if(that.data.userInfo.nickName == that.data.content_datas.response_user[i].agreelist[j])
            {
              that.setData({
                 ['content_datas.response_user['+ i +'].change']:true,
            })
              break;
            }    
          }  
       }   
       that.setData({
        'content_datas.sortfather':(that.data.content_datas.response_user),
       })
      }
    })
    wx.showToast({
      icon:'success',
      title: '刷新成功！'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})